/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package varredor.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import varredor.StartPoint;
import varredor.model.Cenario;
import varredor.model.ComputadorNo;

/**
 *
 * @author Murilo
 */
public class VisualizarVarredoresFrame extends javax.swing.JFrame {

	private Timer timer;
	private Cenario cenario;

	/**
	 * Creates new form VisualizarVarredoresFrame
	 */
	public VisualizarVarredoresFrame() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of
	 * this method is always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jPanel1 = new javax.swing.JPanel();
		jPanel2 = new javax.swing.JPanel();
		adicionarVarredorButton = new javax.swing.JButton();

		setTitle("Computadores na rede");

		jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { { null, null, null, null, null, null, null, null },
			{ null, null, null, null, null, null, null, null }, { null, null, null, null, null, null, null, null },
			{ null, null, null, null, null, null, null, null } }, new String[] { "MAC", "IP", "Verificado", "Em Verificação", "Modificado",
			"On Line", "Data Varredura", "Tempo Varredura" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false, false, false, false, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		jScrollPane1.setViewportView(jTable1);

		getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

		jPanel1.setPreferredSize(new java.awt.Dimension(504, 70));

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
			.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 787, Short.MAX_VALUE));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 70, Short.MAX_VALUE));

		getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

		jPanel2.setPreferredSize(new java.awt.Dimension(504, 70));

		adicionarVarredorButton.setText("Adicionar Varredor");
		adicionarVarredorButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				adicionarVarredorButtonActionPerformed(evt);
			}
		});
		jPanel2.add(adicionarVarredorButton);

		getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 803) / 2, (screenSize.height - 401) / 2, 803, 401);
	}// </editor-fold>//GEN-END:initComponents

	private void adicionarVarredorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarVarredorButtonActionPerformed
		adicionarNovoVarredor();
	}//GEN-LAST:event_adicionarVarredorButtonActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/*
		 * Set the Nimbus look and feel
		 */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. For details see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(VisualizarVarredoresFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(VisualizarVarredoresFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(VisualizarVarredoresFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(VisualizarVarredoresFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/*
		 * Create and display the form
		 */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new VisualizarVarredoresFrame().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton adicionarVarredorButton;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;

	// End of variables declaration//GEN-END:variables

	public void iniciarVarreduraCenario() {

		timer = new Timer(1000, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				timer.stop();
				verificarCenario();
				timer.start();
			}
		});

		timer.start();
	}

	protected void verificarCenario() {
		Cenario cenarioAtual = StartPoint.getINSTANCE().getCenario();

		if (cenarioAtual != null) {
			cenario = cenarioAtual;
		}

		if (cenario != null) {
			List<ComputadorNo> computadores = cenario.getComputadores();

			Object[][] data = new Object[computadores.size()][8];
			int i = 0;
			for (ComputadorNo computadorNo : computadores) {
				data[i] = computadorNo.toArray();
				i++;
			}

			jTable1.setModel(new javax.swing.table.DefaultTableModel(data, new String[] { "MAC", "IP", "Verificado", "Em Verificação", "Modificado",
				"On Line", "Data Varredura", "Tempo Varredura" }) {

				boolean[] canEdit = new boolean[] { false, false, false, false, false, false, false, false };

				public boolean isCellEditable(int rowIndex, int columnIndex) {
					return canEdit[columnIndex];
				}
			});
		}
	}

	private void adicionarNovoVarredor() {
		try {
			StartPoint.getINSTANCE().adicionarNovoVarredor();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
			e.printStackTrace();
		}
	}
}
